package com.huahai.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huahai.annotation.AuthCheck;
import com.huahai.common.BaseResponse;
import com.huahai.common.DeleteRequest;
import com.huahai.constant.UserConstant;
import com.huahai.exception.BusinessException;
import com.huahai.exception.ErrorCode;
import com.huahai.pojo.dto.comment.CommentAddDTO;
import com.huahai.pojo.dto.comment.CommentQueryDTO;
import com.huahai.pojo.entity.Comment;
import com.huahai.pojo.entity.User;
import com.huahai.pojo.vo.comment.CommentVO;
import com.huahai.service.CommentService;
import com.huahai.service.UserService;
import com.huahai.util.ResultUtils;
import com.huahai.util.ThrowUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 评论控制层
 *
 * @author huahai
 */
@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {

    @Resource
    private CommentService commentService;

    @Resource
    private UserService userService;

    /**
     * 添加评论
     *
     * @param commentAddDTO 添加评论参数
     * @param request       请求
     * @return Long 添加的评论id
     */
    @ApiOperation("添加评论")
    @PostMapping("/add")
    public BaseResponse<Long> addComment(@RequestBody CommentAddDTO commentAddDTO, HttpServletRequest request) {
        // 参数校验
        ThrowUtils.throwIf(commentAddDTO == null, ErrorCode.PARAMS_ERROR);
        // 获取当前登录用户
        User loginUser = userService.getLoginUser(request);
        Long commentId = commentService.addComment(commentAddDTO, loginUser);
        return ResultUtils.success(commentId);
    }

    /**
     * 删除评论
     *
     * @param deleteRequest 删除参数
     * @param request       请求
     * @return Boolean 删除成功与否
     */
    @ApiOperation("删除评论")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteComment(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        // 参数校验
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 获取当前登录用户
        User loginUser = userService.getLoginUser(request);
        Boolean result = commentService.deleteComment(deleteRequest, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取单条评论 (管理员)
     *
     * @param commentQueryDTO 评论查询参数
     * @param request         请求
     * @return CommentVO
     */
    @ApiOperation("管理员获取单条评论")
    @PostMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Comment> getCommentById(@RequestBody CommentQueryDTO commentQueryDTO, HttpServletRequest request) {
        // 参数校验
        ThrowUtils.throwIf(commentQueryDTO == null, ErrorCode.PARAMS_ERROR);
        // 获取当前登录用户
        User loginUser = userService.getLoginUser(request);
        // 获取单条评论
        Comment comment = commentService.getCommentById(commentQueryDTO, loginUser);
        return ResultUtils.success(comment);
    }

    /**
     * 根据 id 获取单条评论 （脱敏）
     *
     * @param commentQueryDTO 评论查询参数
     * @param request         请求
     * @return CommentVO
     */
    @ApiOperation("获取脱敏后的单条评论")
    @GetMapping("/get/vo")
    public BaseResponse<CommentVO> getCommentVOById(@RequestBody CommentQueryDTO commentQueryDTO, HttpServletRequest request) {
        // 参数校验
        ThrowUtils.throwIf(commentQueryDTO == null, ErrorCode.PARAMS_ERROR);
        // 获取当前登录用户
        User loginUser = userService.getLoginUser(request);
        // 获取单条评论
        CommentVO commentVO = commentService.getCommentVOById(commentQueryDTO, loginUser, request);
        return ResultUtils.success(commentVO);
    }

    /**
     * 分页获取评论列表 （管理员）
     *
     * @param commentQueryDTO 分页参数
     * @param request         请求
     * @return Page<Comment> 分页后的评论列表
     */
    @ApiOperation("管理员分页获取评论列表")
    @PostMapping("/list")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Comment>> listCommentByPage(@RequestBody CommentQueryDTO commentQueryDTO, HttpServletRequest request) {
        // 参数校验
        ThrowUtils.throwIf(commentQueryDTO == null, ErrorCode.PARAMS_ERROR);
        // 获取当前登录用户
        User loginUser = userService.getLoginUser(request);
        // 分页获取评论列表
        Page<Comment> commentPage = commentService.getCommentPage(commentQueryDTO, loginUser);
        return ResultUtils.success(commentPage);
    }

    /**
     * 分页获取评论列表  (脱敏)
     *
     * @param commentQueryDTO 分页参数
     * @param request 请求
     * @return Page<CommentVO> 分页后的评论列表
     */
    @ApiOperation("分页获取脱敏后的评论列表")
    @PostMapping("/list/vo")
    public BaseResponse<Page<CommentVO>> listCommentVOByPage(@RequestBody CommentQueryDTO commentQueryDTO, HttpServletRequest request) {
        // 参数校验
        ThrowUtils.throwIf(commentQueryDTO == null, ErrorCode.PARAMS_ERROR);
        // 获取当前登录用户
        User loginUser = userService.getLoginUser(request);
        // 分页获取评论列表
        Page<CommentVO> commentVOPage = commentService.getCommentVOPage(commentQueryDTO, loginUser, request);
        return ResultUtils.success(commentVOPage);
    }

}
